package com.hnichr.ump.ecard.controller;


import com.hnichr.ump.common.ecard.entity.EcardBusinessCard;
import com.hnichr.ump.common.ecard.entity.EcardCompany;
import com.hnichr.ump.common.ecard.service.EcardBusinessCardService;
import com.hnichr.ump.common.ecard.service.EcardCompanyService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/ecard/card/v1/")
@Tag(name = "BusinessCardController", description = "电子名片维护接口")
public class BusinessCardController {

    @Autowired
    private EcardBusinessCardService cardService;

    @Autowired
    private EcardCompanyService companyService;


    @GetMapping("list")
    @Operation(summary = "电子卡片列表", description = "获取电子卡片列表（无分页）", method = "GET")
    public Result<List<EcardBusinessCard>> list(@Parameter(description="查询参数") Criteria criteria){
        List<EcardBusinessCard> list = cardService.list(criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "电子卡片列表", description = "电子卡片列表（分页）", method = "GET")
    public Result<Page<EcardBusinessCard>> page(@Parameter(description="查询参数")  Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo){
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        Page<EcardBusinessCard> list = cardService.listPage(page, criteria);
        return Result.OK(list);
    }

    @GetMapping("list/{id}")
    @Operation(summary = "根据ID获取电子卡片", description = "根据ID获取电子卡片", method = "GET")
    public Result<EcardBusinessCard> getById(@Parameter(description="ID") @PathVariable(name = "id", required = true) String id) {
        EcardBusinessCard card = cardService.getById(EcardBusinessCard.class, id);
        EcardCompany company = companyService.getById(EcardCompany.class,card.getCompanyId());
        card.setCompanyName(company.getCompanyName());
        return Result.OK(card);
    }

    @PostMapping("add")
    @Operation(summary = "新增电子卡片", description = "新增电子卡片", method = "POST")
    public Result<EcardBusinessCard> add(@RequestBody EcardBusinessCard card) {
        if(StringUtils.isBlank(card.getAvatarUrl())){
            card.setHiddenAvatar("1");
        }
        if(StringUtils.isBlank(card.getHiddenAvatar())){
            card.setHiddenAvatar("1");
        }
        card.setShareCnt(0);
        cardService.save(card);
        return Result.OK(card);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑电子卡片", description = "编辑电子卡片", method = "PUT")
    public Result<EcardBusinessCard> edit(@RequestBody EcardBusinessCard card) {
        if(StringUtils.isBlank(card.getAvatarUrl())){
            card.setHiddenAvatar("1");
        }
        if(StringUtils.isBlank(card.getHiddenAvatar())){
            card.setHiddenAvatar("1");
        }
        cardService.update(card);
        return Result.OK(card);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除电子卡片", description = "根据id删除电子卡片", method = "DELETE")
    public Result<EcardBusinessCard> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            cardService.remove(EcardBusinessCard.class, key);
        }
        return Result.OK();
    }
}
